﻿using System;
using Microsoft.SharePoint;

namespace SCCL2010
{
    public static class Config
    {

        private const string SPQueryConfigName = "<Where><Contains><FieldRef Name=\"sccl_ConfigName\" /><Value Type=\"Text\">{0}</Value></Contains></Where>";
        private const string STR_Configuration = "Configuration";


         #region Read Values

        public static string ReadConfigValue(SPWeb oWeb, string cValueName)
        {
            return ReadConfigValue(STR_Configuration, oWeb, cValueName);
        }

        public static string ReadConfigValue(string cListName, SPWeb oWeb, string cValueName)
        {
            try
            {
                SPList lstConfiguration = oWeb.Lists[cListName];
                if (lstConfiguration != null)
                {
                    SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                    SPListItemCollection items = lstConfiguration.GetItems(oQuery);
                    if (items != null)
                    {
                        foreach (SPListItem item in lstConfiguration.Items)
                        {
                            if (item["Config Name"] != null)
                            {
                                if (item["Config Name"].ToString().Trim().ToLower() == cValueName.ToLower().Trim())
                                {
                                    if (item["Config Value"] != null)
                                    {
                                        return item["Config Value"].ToString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Config.ReadConfigValue", ex);
            }
            return "";
        }

        public static bool ReadBooleanConfigValue(SPWeb oWeb, string cValueName)
        {
            return ReadBooleanConfigValue(STR_Configuration, oWeb, cValueName);
        }

         public static bool ReadBooleanConfigValue(string cListName, SPWeb oWeb,string cValueName)
        {
            try
            {
                SPList lstConfiguration = oWeb.Lists[cListName];
                SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                SPListItemCollection items = lstConfiguration.GetItems(oQuery);
                if (items.Count > 0)
                {
                    SPItem item = items[0];
                    if (item["Config Value"] != null)
                    {
                        if (item["Config Value"].ToString().Trim().ToLower() == "yes")
                            return true;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Config.ReadBooleanConfigValue", ex);
            }
            return false;
        }

         public static int ReadIntConfigValue(SPWeb oWeb, string cValueName)
         {
             return ReadIntConfigValue(STR_Configuration, oWeb, cValueName);
         }

         public static int ReadIntConfigValue(string cListName, SPWeb oWeb, string cValueName)
         {
             try
             {
                 SPList lstConfiguration = oWeb.Lists[cListName];
                 SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                 SPListItemCollection items = lstConfiguration.GetItems(oQuery);
                 if (items.Count > 0)
                 {
                     SPItem item = items[0];
                     if (item["Config Value"] != null)
                     {
                         string cValue = item["Config Value"].ToString();
                         if (IsInteger(cValue))
                         {
                             return Convert.ToInt32(cValue);
                         }
                         else
                         {
                             //ErrorHandling.LogMessage(oWeb, "ReadIntConfigValue", "Value Found not a Integer" + cValueName);
                             return 0;
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 ErrorHandling.HandleError(oWeb, "Config.ReadIntConfigValue", ex);
             }
             ErrorHandling.LogMessage(oWeb, "ReadIntConfigValue", "No Value Found for " + cValueName);
             return -1;
         }


         public static SPUser ReadUserConfigValue(SPWeb oWeb, string cValueName)
         {
             return ReadUserConfigValue(STR_Configuration, oWeb, cValueName);
         }

         public static SPUser ReadUserConfigValue(string cListName, SPWeb oWeb,string cValueName)
         {
             try
             {
                 SPList lstConfiguration = oWeb.Lists[cListName];
                 SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                 SPListItemCollection items = lstConfiguration.GetItems(oQuery);

                 if (items.Count > 0)
                 {
                     SPListItem item = items[0];
                     if (item["User Value"] != null)
                     {
                         return UsersAndGroups.GetUser(item, "User Value");
                     }
                 }

             }
             catch (Exception ex)
             {
                 ErrorHandling.HandleError(oWeb, "Config.ReadUserConfigValue", ex);
             }
             return null;
         }

         public static SPGroup ReadGroupConfigValue(SPWeb oWeb, string cValueName)
         {
             return ReadGroupConfigValue(STR_Configuration, oWeb, cValueName);
         }

         public static SPGroup ReadGroupConfigValue(string cListName, SPWeb oWeb,string cValueName)
         {
             try
             {
                 SPList lstConfiguration = oWeb.Lists[cListName];
                 SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                 SPListItemCollection items = lstConfiguration.GetItems(oQuery);

                 if (items.Count > 0)
                 {
                     SPListItem item = items[0];
                     if (item["User Value"] != null)
                     {
                         SPFieldUserValue objUserFieldValue = new SPFieldUserValue(oWeb, item["User Value"].ToString());
                         if (objUserFieldValue != null)
                         {
                             SPGroup oGrp = UsersAndGroups.GetGroup(oWeb, objUserFieldValue.LookupValue);
                             return oGrp;
                         }
                     }
                 }

             }
             catch (Exception ex)
             {
                 ErrorHandling.HandleError(oWeb, "Config.ReadGroupConfigValue", ex);
             }
             return null;
         }



        #endregion

         #region Add Values

         public static void AddConfigValue(string cListName, SPWeb oWeb, string ConfigName, string ConfigValue)
         {
             AddConfigValue(cListName, oWeb, "UnKnown", ConfigName, ConfigValue);

         }

         public static void AddConfigValue(SPWeb oWeb, string ConfigName, string ConfigValue)
         {
             AddConfigValue(STR_Configuration, oWeb, "UnKnown", ConfigName, ConfigValue);

         }
         public static void AddConfigValue(SPWeb oWeb, string cCategory, string ConfigName, string ConfigValue)
         {
             AddConfigValue(STR_Configuration, oWeb, cCategory, ConfigName, ConfigValue);
         }

         public static void AddConfigValue(string cListName, SPWeb oWeb, string cCategory, string ConfigName, string ConfigValue)
         {
             try
             {
                 SPList lstConfiguration = oWeb.Lists[cListName];
                 SPListItem newItem = lstConfiguration.Items.Add();
                 newItem["Config Name"] = ConfigName;
                 newItem["Config Value"] = ConfigValue;
                 newItem["Category"] = cCategory;
                 newItem["Title"] = ConfigName;
                 oWeb.AllowUnsafeUpdates = true;
                 newItem.Update();
             }
             catch (Exception ex)
             {
                 ErrorHandling.HandleError(oWeb, "AddConfigValue", ex);
             }
         }


         public static void AddUserConfigValue(SPWeb oWeb, string ConfigName, SPUser ConfigUser)
         {
             AddUserConfigValue(STR_Configuration, oWeb, "UnKnown", ConfigName, ConfigUser);

         }

         public static void AddUserConfigValue(string cListName, SPWeb oWeb, string ConfigName, SPUser ConfigUser)
         {
             AddUserConfigValue(cListName, oWeb, "UnKnown", ConfigName, ConfigUser);

         }

         public static void AddUserConfigValue(SPWeb oWeb, string ConfigName, SPUser ConfigUser, string cCategory)
         {
             AddUserConfigValue(STR_Configuration, oWeb, cCategory, ConfigName, ConfigUser);

         }

         public static void AddUserConfigValue(string cListName, SPWeb oWeb, string cCategory, string ConfigName,SPUser ConfigUser)
        {
            try
            {
                SPList lstConfiguration = oWeb.Lists[cListName];
                SPListItem newItem = lstConfiguration.Items.Add();
                newItem["Config Name"] = ConfigName;
                newItem["User Value"] = ConfigUser;
                newItem["Category"] = cCategory;
                oWeb.AllowUnsafeUpdates = true;
                newItem.Update();
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "AddConfigUserValue", ex);
            }
        }

        #endregion

         #region Ensure Values


         public static void EnsureConfigValue(SPWeb oWeb, string ConfigName, string ConfigValue)
         {
             if (!HasConfigValue(STR_Configuration, oWeb, ConfigName))
             {
                 AddConfigValue(STR_Configuration, oWeb, "UnKnown", ConfigName, ConfigValue);
             }
         }

         public static void EnsureConfigValue(string cListName, SPWeb oWeb, string ConfigName, string ConfigValue)
         {
             if (!HasConfigValue(cListName, oWeb, ConfigName))
             {
                 AddConfigValue(cListName, oWeb, "UnKnown", ConfigName, ConfigValue);
             }
         }

         public static void EnsureConfigValue(SPWeb oWeb, string ConfigName, string ConfigValue, string cCategory)
         {
             if (!HasConfigValue(STR_Configuration, oWeb, ConfigName))
             {
                 AddConfigValue(STR_Configuration, oWeb, cCategory, ConfigName, ConfigValue);
             }

         }

         public static void EnsureConfigValue(string cListName, SPWeb oWeb, string ConfigName, string ConfigValue, string cCategory)
         {
             if (!HasConfigValue(cListName, oWeb, ConfigName))
             {
                 AddConfigValue(cListName, oWeb, cCategory, ConfigName, ConfigValue);
             }
         }


         public static void EnsureUserConfigValue(SPWeb oWeb, string ConfigName, SPUser ConfigValue)
         {
             if (!HasConfigValue(STR_Configuration, oWeb, ConfigName))
             {

                 AddUserConfigValue(STR_Configuration, oWeb, "UnKnown", ConfigName, ConfigValue);
             }
         }

         public static void EnsureUserConfigValue(string cListName, SPWeb oWeb, string ConfigName, SPUser ConfigValue)
         {
             if (!HasConfigValue(cListName, oWeb, ConfigName))
             {
                 AddUserConfigValue(cListName, oWeb, "UnKnown", ConfigName, ConfigValue);
             }
         }

         public static void EnsureUserConfigValue(SPWeb oWeb, string ConfigName, SPUser ConfigValue, string cCategory)
         {
             if (!HasConfigValue(STR_Configuration, oWeb, ConfigName))
             {
                 AddUserConfigValue(STR_Configuration, oWeb, cCategory, ConfigName, ConfigValue);
             }

         }

         public static void EnsureUserConfigValue(string cListName, SPWeb oWeb, string ConfigName, SPUser ConfigValue, string cCategory)
         {
             if (!HasConfigValue(cListName, oWeb, ConfigName))
             {
                 AddUserConfigValue(cListName, oWeb, cCategory, ConfigName, ConfigValue);
             }
         }

         #endregion

         #region Update Values

         public static bool UpdateConfigValue(SPWeb oWeb, string cValueName, string cNewValue)
         {
             return UpdateConfigValue(STR_Configuration, oWeb, cValueName, cNewValue);
         }

         public static bool UpdateConfigValue(string cListName, SPWeb oWeb, string cValueName, string cNewValue)
         {
             try
             {
                 SPList lstConfiguration = oWeb.Lists[cListName];
                 SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                 SPListItemCollection items = lstConfiguration.GetItems(oQuery);

                 if (items.Count > 0)
                 {
                     SPItem item = items[0];
                     item["Config Value"] = cNewValue;
                     item.Update();
                     return true;
                 }
             }
             catch (Exception ex)
             {
                 ErrorHandling.HandleError(oWeb, "Config.UpdateConfigValue", ex);
             }
             return false;
         }

         #endregion

          public static bool IsInteger(string theValue)
        {
            try
            {
                Convert.ToInt32(theValue);
                return true;
            }
            catch
            {
                return false;
            }
        }

          public static bool HasConfigValue(string cListName, SPWeb oWeb, string cValueName)
          {
              try
              {
                  SPList lstConfiguration = oWeb.Lists[cListName];
                  SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                  SPListItemCollection items = lstConfiguration.GetItems(oQuery);
                  if (items.Count > 0)
                  {
                      return true;
                  }
              }
              catch (Exception ex)
              {
                  ErrorHandling.HandleError(oWeb, "Config.HasConfigValue", ex);
              }
              return false;
          }

          public static bool HasConfigValue(SPWeb oWeb, string cValueName)
          {
              try
              {
                  SPList lstConfiguration = oWeb.Lists[STR_Configuration];
                  SPQuery oQuery = new SPQuery { Query = String.Format(SPQueryConfigName, cValueName) };
                  SPListItemCollection items = lstConfiguration.GetItems(oQuery);
                  if (items.Count > 0)
                  {
                      return true;
                  }
              }
              catch (Exception ex)
              {
                  ErrorHandling.HandleError(oWeb, "Config.HasConfigValue", ex);
              }
              return false;
          }



        


    }
}
